package com.shelpe.services.sinbadgateway.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;

@FeignClient(name = "${feign.sinbadserver.name}", url = "${feign.sinbadserver.url}")
public interface SinbadClient {

	@RequestMapping(method = RequestMethod.POST, path = "/sinbadservice/adgroups", consumes = "application/json;charset=UTF-8")
	AdgroupDto createAdgroup(@RequestBody AdgroupDto dto);

	@RequestMapping(method = RequestMethod.PUT, path = "/sinbadservice/adgroups/{adgroup_id}/@status", consumes = "application/json;charset=UTF-8")
	AdgroupDto updateAdgroupStatus(@PathVariable("adgroup_id") long adgroupID, @RequestBody AdgroupDto dto);

	@RequestMapping(method = RequestMethod.POST, path = "/sinbadservice/adgroups/{adgroup_id}", consumes = "application/json;charset=UTF-8")
	AdgroupDto findAdgroup(@PathVariable("adgroup_id") long adgroupID);

	@RequestMapping(method = RequestMethod.PUT, path = "/sinbadservice/adgroups/{adgroup_id}", consumes = "application/json;charset=UTF-8")
	AdgroupDto updateAdgroup(@PathVariable("adgroup_id") long adgroupID, @RequestBody AdgroupDto dto);

	@RequestMapping(method = RequestMethod.GET, path = "/sinbadservice/adgroups/{adgroup_id}/keywordStates", consumes = "application/json;charset=UTF-8")
	List<KeywordState> findKeywordStates(@PathVariable("adgroup_id") long adgroupID, @RequestParam("days") int days);

	@RequestMapping(method = RequestMethod.GET, path = "/sinbadservice/adgroups/{adgroup_id}/keywords", consumes = "application/json;charset=UTF-8")
	ListResponse<Keyword> findKeyworsdByAdgroupID(@PathVariable("adgroup_id") long adgroupID);

	@RequestMapping(method = RequestMethod.GET, path = "/sinbadservice/users/{userId}/summaries/metric")
	MaxPriceDto summariesMetric(@PathVariable("userId") long userId, @RequestParam("startDate") String startDate,
			@RequestParam("endDate") String endDate);

	@RequestMapping(method = RequestMethod.PUT, path = "/sinbadservice/adgroups/{adgroup_id}/actions", consumes = "application/json;charset=UTF-8")
	AdgroupResponse updateAdgroupWithKeyword(@PathVariable("adgroup_id") long adgroupID);
	
	@Data
	@JsonInclude(Include.NON_NULL)
	public class ListResponse<T> {
		private boolean status;
		private int total;
		private List<T> datas;
		
		private SimbaProxyBaseDto.Error error;
	}
	
	@Data
	@JsonInclude(Include.NON_NULL)
	public static class AdgroupResponse {
		private boolean status;
		private String message;
	}

	@Data
	public static class MaxPriceDto {
		@JsonProperty("taobaoUserId")
		private long userID;
		private Float cpc;
		private float ctr;
		private int impressions;
		private int click;
		private float cpm;
		private int cost;
	}

	@Data
	public class KeywordReq {
		private List<Keyword> words;

		public void addWord(Keyword word) {
			if (words != null) {
				this.words = new ArrayList<Keyword>();
			}
			this.words.add(word);
		}
	}

	@Data
	public class Keyword {
		@JsonProperty("keywordId")
		private long keywordID;
		@JsonProperty("matchscope")
		private int matchScope;
		private String word;
		@JsonProperty("maxPrice")
		private int maxPrice;
	}

	@Data
	public static class AdgroupDto extends SimbaProxyBaseDto {

		@JsonProperty("adgroup_id")
		private long adgroupID;

		@JsonProperty("user_id")
		private long userID;
		@JsonProperty("campaign_id")
		private long campaignID;
		@JsonProperty("product_id")
		private long productID;
		@JsonProperty("title")
		private String productName;
		@JsonProperty("img_url")
		private String productUrl;
		private int status;
		@JsonProperty("drive_type")
		private int driveType;
		@JsonProperty("default_price")
		private int defaultPrice;

		@JsonProperty("creative_title")
		private String creativeName;
		@JsonProperty("creative_img_url")
		private String creativeImgUrl;
		/**
		 * below fields are only for update
		 */
		@JsonProperty("line_status")
		private String lineStatus;

		@JsonProperty("maintainDate")
		@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd")
		private Date maintainedDate;
		
		private int limit;
	}

	@Data
	public static class KeywordState {
		@JsonProperty("keyword_id")
		private long keywordID;
		private String word;
		@JsonProperty("max_price")
		private float maxPrice;
		@JsonProperty("impressions")
		private int impression;// 展现量
		@JsonProperty("matchscope")
		private int matchScope;
		private int click;// 点击量
		@JsonProperty("ctr")
		private float clickRatio;// 点击率
		private float cpc;// 平均点击花费
		private float cpm;
		private float cvr;// 点击转化率
		@JsonProperty("avgpos")
		private int productRanking;// 宝贝排名
		private int qscore;// 质量得分
		@JsonProperty("wireless_qscore")
		private int mobileQscore;// 移动质量得分
		private float cost;// 总花费
		@JsonProperty("sales")
		private float sale;// 总金额
		@JsonProperty("total_pay_count")
		private int payCount;// 成交笔数
		private float roi;// 投入产出比
		@JsonProperty("fav_count")
		private int favCount;// 收藏数
		@JsonProperty("fav_count_rate")
		private float favCountRatio;// 收藏率
		@JsonProperty("cart_count")
		private int cartCount;// 购物车数
		@JsonProperty("cart_count_rate")
		private float cartCountRatio;// 购物车率

		@JsonIgnore
		private float qpr;
	}
}
